/**
 * CEAport framework to create open APIs for communications enable applications.
 * https://ceaport.googlecode.com
 *
 * Copyright (c) 2008 by respective authors (see below).
 *
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Contributors:
 * Michael Weiss (michael.r.weiss@gmail.com)
 * 
 */
package org.ceaport.rest.server.events;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.red5.server.net.rtmp.RTMPConnection;
import org.red5.server.net.rtmp.codec.RTMP;
import org.red5.server.api.so.IClientSharedObject;
import org.red5.server.api.so.ISharedObjectBase;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 * Sample class that uses the client mode of the RTMP library
 * to connect to chat application.
 *
 * @author michael.r.weiss
 */
public class ChatEventStream extends EventStream {
	
	protected IClientSharedObject chatSO;
	
	public ChatEventStream(String host, String room) {
		super(host, room);
	}

	public String getApplication() {
		return "chatServer";
	}
	
	public void connectionOpened(RTMPConnection conn, RTMP state) {
		super.connectionOpened(conn, state);
		chatSO = subscribeSharedObject(conn, "chatSO");
	}
		    
	public void onSharedObjectSend(ISharedObjectBase so, String method, List params) {
		if (method.equals("receiveNewMessage")) {
			Map<String, String> attributes = getAttributes();			
			attributes.put("user", "" + params.get(0));
			attributes.put("message", "" + params.get(1));
			consumer.processEvent("chat", attributes);
		}
	}

}